package cn.hx.mallBlog.controller;

import cn.hx.mallBlog.entity.CmsTopic;
import cn.hx.mallBlog.entity.CmsTopicCategory;
import cn.hx.mallBlog.entity.CmsTopicComment;
import cn.hx.mallBlog.service.CmsTopicCategoryService;
import cn.hx.mallBlog.service.CmsTopicCommentService;
import cn.hx.mallBlog.service.CmsTopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.SourceTree;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Source;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 话题表(CmsTopic)表控制层
 *
 * @author makejava
 * @since 2022-11-30 15:36:10
 */
@Controller
public class CmsTopicController {
    /**
     * 服务对象
     */
    @Resource
    private CmsTopicService cmsTopicService;
    @Resource
    private CmsTopicCategoryService cmsTopicCategoryService;
    @Resource
    private CmsTopicCommentService cmsTopicCommentService;

    @GetMapping({"blog-right-sidebar", "/"})
    public Object blogs(Model model, PageInfo pageInfo, CmsTopic cmsTopic) {
        if (pageInfo.getPageNum() == 0) {
            pageInfo.setPageNum(1);
        }
        if (pageInfo.getPageSize() == 0) {
            pageInfo.setPageSize(5);
        }
        PageHelper.startPage(pageInfo);
        List<CmsTopic> cmsTopics = cmsTopicService.queryAll(cmsTopic);
        if (cmsTopics.size()==0){
            model.addAttribute("m","未搜索到相关博客");
        }
        PageInfo pageInfo1 = new PageInfo(cmsTopics);
        model.addAttribute("cmsTopicsList", pageInfo1);
        model.addAttribute("createTimeList", cmsTopicService.timeList());
        List<CmsTopicCategory> cmsTopicCategories = cmsTopicCategoryService.queryAll(null);
        model.addAttribute("cmsTopicCategoryList", cmsTopicCategories);
        List<CmsTopic> allByLimit = cmsTopicService.findAllByLimit(0, 3);
        model.addAttribute("timeList", allByLimit);
        return "blog-right-sidebar";
    }


    @GetMapping("/selectTopic/{id}")
    public Object selectTopic(@PathVariable Integer id, Model model, HttpSession httpSession) {
        httpSession.setAttribute("id", id);
        CmsTopic cmsTopic = cmsTopicService.queryById(id);
        Integer readCount = cmsTopic.getReadCount();
        Integer readCount1 = readCount + 1;
        cmsTopic.setReadCount(readCount1);
        cmsTopicService.update(cmsTopic);
        CmsTopic cmsTopic1 = cmsTopicService.queryById(id);
        model.addAttribute("details", cmsTopic1);
        if(cmsTopic1==null){
            model.addAttribute("message","未找到相关博客信息");
        }
        List<CmsTopicCategory> cmsTopicCategories = cmsTopicCategoryService.queryAll(null);
        model.addAttribute("cmsTopicCategoryList", cmsTopicCategories);
        model.addAttribute("createTimeList", cmsTopicService.timeList());
        List<CmsTopic> allByLimit = cmsTopicService.findAllByLimit(0, 3);
        model.addAttribute("timeList", allByLimit);
        CmsTopicComment cmsTopicComment = new CmsTopicComment();
        cmsTopicComment.setViewId(1);
        cmsTopicComment.setTopicId(Long.valueOf(id));
        List<CmsTopicComment> cmsTopicComments = cmsTopicCommentService.queryAll(cmsTopicComment);
        model.addAttribute("cmsTopicComments", cmsTopicComments);
        model.addAttribute("count", cmsTopicComments.size());
        httpSession.setAttribute("id", id);
        return "blog-details-sidebar";
    }

    /*较老帖子*/
    @GetMapping("/oldTopic/{id}")
    public Object oldTopic(Model model, @PathVariable Integer id,HttpSession session) {
        CmsTopic cmsTopic = cmsTopicService.queryById(id);
        Date createTime = cmsTopic.getCreateTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatTime = format.format(createTime);
        CmsTopic topic = cmsTopicService.queryByOldTime(formatTime);
        if (topic != null) {
            Integer readCount = topic.getReadCount();
            Integer readCount1 = readCount + 1;
            topic.setReadCount(readCount1);
            topic.setLookTime(new Date());
            //cmsTopic.setAttendCount(cmsTopic.getAttentionCount()+1);
            cmsTopicService.update(topic);
        }
        CmsTopicComment cmsTopicComment = new CmsTopicComment();
        cmsTopicComment.setViewId(1);
        cmsTopicComment.setTopicId(Long.valueOf(id));
        List<CmsTopicComment> cmsTopicComments = cmsTopicCommentService.queryAll(cmsTopicComment);
        model.addAttribute("cmsTopicComments", cmsTopicComments);
        model.addAttribute("count", cmsTopicComments.size());
        session.setAttribute("id", id);
        return getObject(model, topic);
    }

    /*较新帖子*/
    @GetMapping("/lastTopic/{id}")
    public Object lastTopic(Model model, @PathVariable Integer id, HttpSession session) {
        CmsTopic cmsTopic = cmsTopicService.queryById(id);
        Date createTime = cmsTopic.getCreateTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatTime = format.format(createTime);
        CmsTopic topic = cmsTopicService.queryByCreateTime(formatTime);
        if (topic != null) {
            Integer readCount = topic.getReadCount();
            Integer readCount1 = readCount + 1;
            topic.setReadCount(readCount1);
            topic.setLookTime(new Date());
            cmsTopicService.update(topic);
        }
        CmsTopicComment cmsTopicComment = new CmsTopicComment();
        cmsTopicComment.setViewId(1);
        cmsTopicComment.setTopicId(Long.valueOf(id));
        List<CmsTopicComment> cmsTopicComments = cmsTopicCommentService.queryAll(cmsTopicComment);
        model.addAttribute("cmsTopicComments", cmsTopicComments);
        model.addAttribute("count", cmsTopicComments.size());
        session.setAttribute("id", id);
        return getObject(model, topic);
    }

    private Object getObject(Model model, CmsTopic topic) {
        List<CmsTopicCategory> cmsTopicCategories = cmsTopicCategoryService.queryAll(null);
        model.addAttribute("cmsTopicCategoryList", cmsTopicCategories);

        List<CmsTopic> allByLimit = cmsTopicService.findAllByLimit(0, 3);
        model.addAttribute("createTimeList", cmsTopicService.timeList());
        model.addAttribute("timeList", allByLimit);
        if (topic == null) {
            return "redirect:/blog-right-sidebar";
        } else {
            model.addAttribute("details", topic);
            return "blog-details-sidebar";
        }
    }


    @PostMapping("blog-right-sidebar")
    public String select(ModelMap map, CmsTopic cmsTopic){
        map.addAttribute("cmsTopicsList",cmsTopicService.queryAll(cmsTopic));
        return "blog-right-sidebar";
    }

    @GetMapping("selectByCategoryId")
    public String selectByCategoryId(Model model, @RequestParam("categoryId") Integer id, PageInfo pageInfo) {
        if (pageInfo.getPageNum() == 0) {
            pageInfo.setPageNum(1);
        }
        if (pageInfo.getPageSize() == 0) {
            pageInfo.setPageSize(5);
        }
        PageHelper.startPage(pageInfo);
        List<CmsTopic> cmsTopics = cmsTopicService.queryByCategoryIdCmsTopic(id);
        PageInfo pageInfo1 = new PageInfo(cmsTopics);
        model.addAttribute("createTimeList", cmsTopicService.timeList());
        model.addAttribute("cmsTopicsList", pageInfo1);
        List<CmsTopicCategory> cmsTopicCategories = cmsTopicCategoryService.queryAll(null);
        model.addAttribute("cmsTopicCategoryList", cmsTopicCategories);
        List<CmsTopic> allByLimit = cmsTopicService.findAllByLimit(0, 3);
        model.addAttribute("timeList", allByLimit);
        return "blog-right-sidebar";
    }

    @GetMapping("CreateLookTime")
    @ResponseBody
    public String CreateLookTime(Integer id) {
        CmsTopic cmsTopic = new CmsTopic();
        cmsTopic.setId(id);
        cmsTopic.setLookTime(new Date());
        cmsTopicService.update(cmsTopic);
        return "";
    }
}